package com.zlzxm.shoesmarket.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.zlzxm.shoesmarket.TestConstant;
import com.zlzxm.shoesmarket.globle.Constant;
import com.zlzxm.shoesmarket.globle.UserManager;
import com.zlzxm.shoesmarket.net.BaseCallBack;
import com.zlzxm.shoesmarket.net.RetrofitClient;
import com.zlzxm.shoesmarket.net.repository.InvRepository;
import com.zlzxm.shoesmarket.net.repository.WareRepository;
import com.zlzxm.shoesmarket.net.respone.BaseResponse;
import com.zlzxm.shoesmarket.net.respone.RpRepository;
import com.zlzxm.shoesmarket.net.respone.RpRepositoryGoods;
import com.zlzxm.shoesmarket.util.RxAndroidHelp;
import com.zlzxm.shoesmarket.view.adapter.entity.EntityRepositoryGoods;
import com.zlzxm.shoesmarket.view.adapter.entity.EntityRepositoryInfo;
import com.zlzxm.shoesmarket.view.window.wheelpick.EntityOption;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;

/**
 * CREATE BY zlzxm ON 4/8/21
 */
public class VMRepository extends BaseViewModel {


    private static final String TAG = "VMRepository";

    private WareRepository wareRepository = new WareRepository();

    private InvRepository invRepository = new InvRepository();

    private final MutableLiveData<List<EntityRepositoryInfo>> liveInfo = new MutableLiveData<>();

    private final MutableLiveData<List<EntityRepositoryGoods>> liveGoods = new MutableLiveData<>();

    private final MutableLiveData<List<EntityOption>> liveRepository = new MutableLiveData<>();

    private final MutableLiveData<String> liveSelectRepository = new MutableLiveData<>();

    private final MutableLiveData<Boolean> liveCanSelectRepository = new MutableLiveData<>();


    public VMRepository() {

        liveInfo.setValue(createInitInfo(null,null,
                null,null
                ,null, null,
                null,null));
    }

    public MutableLiveData<List<EntityRepositoryInfo>> getLiveInfo() {
        return liveInfo;
    }

    public MutableLiveData<List<EntityRepositoryGoods>> getLiveGoods() {
        return liveGoods;
    }

    public MutableLiveData<List<EntityOption>> getLiveRepository() {
        return liveRepository;
    }

    public MutableLiveData<String> getLiveSelectRepository() {
        return liveSelectRepository;
    }

    public MutableLiveData<Boolean> getLiveCanSelectRepository() {
        return liveCanSelectRepository;
    }

    public void setSelectedRepository(EntityOption entityOption){

        liveSelectRepository.setValue(entityOption.getName());
    }

    public void requestGoods(String ckId){

        invRepository.list(null,ckId,null)
                .compose(RxAndroidHelp.turn())
                .subscribe(new Consumer<RpRepositoryGoods>() {
                    @Override
                    public void accept(RpRepositoryGoods rpRepositoryGoods) throws Throwable {

                        if (rpRepositoryGoods.getPageInfo()!=null){

                            List<RpRepositoryGoods.Entity> entities =  rpRepositoryGoods.getPageInfo().getList();

                            List<EntityRepositoryGoods> list = new ArrayList<>();

                            for (RpRepositoryGoods.Entity entity : entities) {

                                EntityRepositoryGoods entityRepositoryGoods = new EntityRepositoryGoods();
                                entityRepositoryGoods.setTitle(entity.getTitle());
                                entityRepositoryGoods.setFace(RetrofitClient.BASE_URL+entity.getImages().substring(0,entity.getImages().indexOf(";")));
                                entityRepositoryGoods.setDesc(entity.getArticleNumber());
                                entityRepositoryGoods.setNum(entity.getNumber());
                                entityRepositoryGoods.setCost(entity.getPrice());
                                entityRepositoryGoods.setProfit(entity.getActualProfit());
                                entityRepositoryGoods.setRealProfit(entity.getActualProfit());

                                list.add(entityRepositoryGoods);

                            }

                            liveGoods.setValue(list);
                        }

                    }
                }, throwableCallback);


    }

    public void requestRepository(){

        wareRepository.ware(UserManager.getInstance().getId())
                .compose(RxAndroidHelp.turn())
                .subscribe(new BaseCallBack<List<RpRepository>>() {
                    @Override
                    protected void succeed(String msg, List<RpRepository> rpRepositories) {

                        List<UserManager.Repository> repository = new ArrayList<>();

                        for (RpRepository rpRepository : rpRepositories) {

                            UserManager.Repository repository1 = new UserManager.Repository();
                            repository1.setId(rpRepository.getId());
                            repository1.setName(rpRepository.getCkname());

                            repository.add(repository1);

                        }

                        UserManager.getInstance().getUser().setRepositories(repository);

                        if(repository.size()>0){
                            liveSelectRepository.setValue(repository.get(0).getName());
                            requestGoods(repository.get(0).getId());
                        }
                    }

                    @Override
                    protected void failed(String msg) {

                    }

                    @Override
                    protected void notLogin() {

                    }
                },throwableCallback);



    }

    //<editor-fold defaultState="collapsed" desc="私有方法">

    //设置仓库信息
    private  List<EntityRepositoryInfo> createInitInfo(String goodsSort, String goodsCost,
                                                       String goodsProfit, String realProfit,
                                                       String goodsNum, String goodsTotal,
                                                       String profitRate, String brokerage){

        List<EntityRepositoryInfo> entities = new ArrayList<>();

        entities.add(new EntityRepositoryInfo(EntityRepositoryInfo.PRODUCT_SORT,goodsSort));
        entities.add(new EntityRepositoryInfo(EntityRepositoryInfo.PRODUCT_COST,goodsCost));
        entities.add(new EntityRepositoryInfo(EntityRepositoryInfo.PRODUCT_PROFIT,goodsProfit));
        entities.add(new EntityRepositoryInfo(EntityRepositoryInfo.REAL_PROFIT,realProfit));
        entities.add(new EntityRepositoryInfo(EntityRepositoryInfo.PRODUCT_NUM,goodsNum));
        entities.add(new EntityRepositoryInfo(EntityRepositoryInfo.PRODUCT_TOTAL,goodsTotal));
        entities.add(new EntityRepositoryInfo(EntityRepositoryInfo.PROFIT_RATE,profitRate));
        entities.add(new EntityRepositoryInfo(EntityRepositoryInfo.BROKERAGE,brokerage));

        return entities;
    }




    // </editor-fold>


//<editor-fold defaultState="collapsed" desc="测试数据">


    private List<EntityRepositoryGoods> createGoodList(){

        List<EntityRepositoryGoods> list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {

            EntityRepositoryGoods entityRepositoryGoods =   new EntityRepositoryGoods();

            entityRepositoryGoods.setCost(i+"2");
            entityRepositoryGoods.setNum(i+"2");
            entityRepositoryGoods.setDesc("货号：xxxxxxxx  品牌：xxxxx");
            if(i != 3) {
                entityRepositoryGoods.setFace(TestConstant.PIC_TWO);
            }
            entityRepositoryGoods.setTitle("我是耐克？？？？？");

            if(i % 2==0) {
                entityRepositoryGoods.setProfit("+1212");
                entityRepositoryGoods.setRealProfit("+1212");

            }else {

                entityRepositoryGoods.setProfit("-1212");
                entityRepositoryGoods.setRealProfit("-1212");

            }
            list.add(entityRepositoryGoods);
        }

        return  list;

    }


    private List<EntityOption> createRepositoryList(){

        List<EntityOption> list = new ArrayList<>();

        for (int i = 0; i < 50; i++) {

            list.add(new EntityOption(0+"","仓库"+i));
        }

        return  list;
    }


// </editor-fold>

}
